<?php
class SessionHandler {

    // ATTRIBUTE
    public $isOn;
    public $isLoggedIn;
    // sesi yang disimpan
    public $activeUser = NULL;
    public $id_role;
    public $username;
    public $password;
    public $fullname;
    public $img_location;
    public $birthdate;
    public $join_date;
    public $wall_role;
    public $location;

    // METHOD
    // konstruktor
    public function __construct() {
        $this->isOn = session_start();
        if ($this->isOn) {
            $this->isLoggedIn = false;
            $this->getSession();
        } else {
            echo "Sesi tidak dapat dibuat";
        }
    }

    // fungsi untuk mengambil data sesi
    public function getSession() {
        if (isset($_SESSION['activeUser'])) {
            $this->isLoggedIn = true;
            $this->activeUser = $_SESSION['activeUser'];
            $this->id_role = $this->activeUser['id_role'];
            $this->username = $this->activeUser['username'];
            $this->password = $this->activeUser['password'];
            $this->fullname = $this->activeUser['fullname'];
            $this->img_location = $this->activeUser['img_location'];
            $this->birthdate = $this->activeUser['birthdate'];
            $this->join_date = $this->activeUser['join_date'];
            $this->wall_role = $this->activeUser['wall_role'];
            $this->location = $this->activeUser['location'];
        }
    }

    // fungsi untuk men-set semua sesi yang dibutuhkan
    public function setSession($activeUser) {
        $this->activeUser = $activeUser;
        $this->id_role = $this->activeUser['id_role'];
        $this->username = $this->activeUser['username'];
        $this->password = $this->activeUser['password'];
        $this->fullname = $this->activeUser['fullname'];
        $this->img_location = $this->activeUser['img_location'];
        $this->birthdate = $this->activeUser['birthdate'];
        $this->join_date = $this->activeUser['join_date'];
        $this->wall_role = $this->activeUser['wall_role'];
        $this->location = $this->activeUser['location'];
        $_SESSION['activeUser'] = $this->activeUser;
    }

    // menghapus sesi saat logout
    public function destroySession() {
        unset($_SESSION['activeUser']);
        session_destroy();
    }

}

?>
